package com.dmy.chat.server.netty.command;

import com.dmy.chat.server.netty.UserChannelBind;
import com.dmy.chat.server.netty.coder.WebSocketEncoder;
import com.dmy.chat.server.netty.command.interceptor.ChatInterceptor;
import com.dmy.chat.server.netty.message.Messages;
import io.netty.channel.Channel;

import java.util.LinkedList;
import java.util.List;

/**
 * create by YCQ for 2020/1/15 11:23
 */
public abstract class  AbstractUserEventCommand implements UserEventCommand {
    protected static final WebSocketEncoder ENCODER = new WebSocketEncoder();
    private final List<ChatInterceptor<Messages>> pipeline = new LinkedList<>();
    protected UserChannelBind channelBind;
    protected Channel channel;
    protected Messages messages;

    @Override
    public List<ChatInterceptor<Messages>> pipeline(){
        return pipeline;
    }

    public AbstractUserEventCommand(UserChannelBind channelBind, Channel channel, Messages messages) {
        this.channelBind = channelBind;
        this.channel = channel;
        this.messages = messages;
    }

    @Override
    public void exec() {

        // 执行前置处理亲
        for (ChatInterceptor<Messages> interceptor : pipeline) {
            interceptor.prevHandler(channel,messages);
        }

        // 调用处理方法
        this.exec0();

        // 执行后置处理器
        for (ChatInterceptor<Messages> interceptor : pipeline) {
            interceptor.postHandler(channelBind,channel,messages);
        }
    }
    public abstract void exec0();
}
